﻿import { Component, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementGetAuditEditComponent } from './requirement-get-audit_edit.component'
import { TestDataDto,UsingFormDetailDto, UsingFormServiceProxy,UsingFormState,UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { AbpSessionService } from '@abp/session/abp-session.service';

@Component({
    templateUrl: './requirement-get-auditall.component.html',
    styleUrls: ['./requirement-get-audit.component.less'],
    animations: [appModuleAnimation()]
})
/**
 * 需求领用
 */
export class RequirementGetAuditAllComponent extends AppComponentBase{
    @ViewChild('requirementGetAuditEditComponent') requirementGetAuditEditComponent: RequirementGetAuditEditComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    tabObject:{
        filterText:""
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';
    hidden = true;
    userName :string;

    constructor(
        injector: Injector,
        private _usingFormServiceProxy: UsingFormServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _abpSessionService: AbpSessionService) {
        super(injector);
    }
    addTab(usingFormDto: TestDataDto): void {

    }
    clearTab(): void {

    }

    getUserName(): void {
        let id:number = this._abpSessionService.userId;
        this._userServiceProxy.getUserForEdit(id) .pipe(finalize(() => { }))
        .subscribe(result=> {
            this.userName = result.user.userName;
            this.getUsingFormList();
        });
    }
    getUsingFormList(event?: LazyLoadEvent) {
        if(this.userName == undefined){
            // 没有用户，则先找用户。
            this.getUserName();
            return;
        }
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();
        this._usingFormServiceProxy.getAllUsingDetails(
            undefined,//state
            undefined,//startuser 领用人 看谁的
            undefined, //currenthandle 谁负责的
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();

            });

    }
    getState(value):string{
        return UsingFormState[value];
    }
    deleteDetailOfForm(record:UsingFormDetailDto): void {
        this.message.confirm(
            '删除该领用申请？',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this.primengTableHelper.showLoadingIndicator();
                    this._usingFormServiceProxy.deleteDetailOfForm(record.id)
                        .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                            this.primengTableHelper.hideLoadingIndicator();
                            this.getUsingFormList();
                        });
                }
            }
        );
    }

    /**
     * 需求包提交审核
     * @param record 
     */
    toNext(record: UsingFormDetailDto) {
        this.requirementGetAuditEditComponent.show(record.id,record.state);
    }
    /**
     * 需求领用出库
     * @param record 
     */
    toExecuting(record: UsingFormDetailDto) {
        this.requirementGetAuditEditComponent.show(record.id,record.state);
    }
    /**
     * 完成申请单
     * @param record 
     */
    toFinish(record: UsingFormDetailDto) {
        this.requirementGetAuditEditComponent.show(record.id,record.state);
    }

    getUsingFormStateStateString(state:number):string{
        return UsingFormState[state];
    }

    onResize(event): void {
    }

    ngOnInit(): void {
        const self = this;
    }
}
